Import mojo
Import diddy
Import diddy.collections
Import model

Class InputButton Extends Sprite
	Global MAX_BUTTONS% = 100
	
	Field event:ButtonEvent 
	Field highlight = false
	Field pressed = false
	Field hoverImage:Int
	Field mainImage:Int
	Field enabled:Bool = true
	
	Function Create:InputButton(gi:GameImage, x#, y#, event:ButtonEvent, frame:Int=0, buttonsList:ArrayList<InputButton> )
	
		Local button:InputButton = new InputButton()
	
		button.image = gi
		button.image.MidHandle(true)
		button.x = x
		button.y = y
		button.alpha = 1
		button.SetHitBox(-gi.w2, -gi.h2, gi.w, gi.h)
		button.event = event
		button.mainImage = frame
		button.enabled = true
		buttonsList.Add(button)
		
		Return button
	End
	
	Function DrawAll:Void(buttonsList:ArrayList<InputButton>)
				For Local ndx:Int=0 Until buttonsList.Size
			Local val:InputButton = buttonsList.Get(ndx)
			val.Draw()
		End					
	End
	
	Function UpdateAll:Void(buttonsList:ArrayList<InputButton>)
		For Local ndx:Int=0 Until buttonsList.Size
			Local val:InputButton = buttonsList.Get(ndx)
			val.Update()
		End	
	End	
	
	Method Draw:Void()
		Local offset:Int = 0
		
	'	alpha = 0.9
		if(enabled=False)
			Return
		EndIf
		
		PushMatrix
		if(highlight)
			alpha += +0.2
		ElseIf (pressed)
			offset = 1
		Else
		'	alpha = 0.9		
		Endif
		PopMatrix
		
		frame=mainImage
		Super.Draw(offset, offset, true)	
		
		if(highlight)
			frame=hoverImage
			Super.Draw(offset, offset, true)	
		
		Endif
	End
	
	Method Update:Void()
		highlight=false
		pressed=false

		if RectsOverlap(x + hitBoxX, y + hitBoxY, hitBoxWidth, hitBoxHeight, 
						game.mouseX, game.mouseY, 1, 1)
			If game.MouseHit()							
				event.OnClicked()
			ElseIf MouseDown()
				pressed=true				
			Else
				highlight=true
			EndIf
		End
	
	End
	

	
End

Class ButtonEvent Abstract
	Method OnClicked:Void() Abstract
End





